package com.example.vspm.service.impl;

import com.example.vspm.entity.Person;
import com.example.vspm.entity.Receiver;
import com.example.vspm.entity.Result;
import com.example.vspm.mapper.PersonMapper;
import com.example.vspm.service.PersonService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class PersonServiceImpl implements PersonService {
    @Autowired
    PersonMapper personMapper;

    //登录
    @Override
    public Result login(Person person, HttpSession session) {
        Result res = new Result();
        res.setCode(-1);
        res.setDetail(null);
        try {
            Integer pid = personMapper.loginPerson(person);
            if (pid == null) {
                res.setCode(-1);
                res.setMsg("用户名或密码错误,请重新填写");
            } else {
                person = personMapper.findPerson(pid);
                res.setCode(200);
                res.setMsg("登录成功");
                res.setDetail(person);
                session.setAttribute("person", person);
                Person person1= (Person)session.getAttribute("person");
                System.out.println(person1.getPersonName() + person1.getTelephone());
            }

        } catch (Exception e) {
            res.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public Person logPerson(String personName,  String password) {
        return personMapper.logPerson(personName,password);
    }

    @Override
    public void register(Person person) {

    }

    @Override
    public Person selectPerson(int tid) {
        return personMapper.findPerson(tid);
    }
}
